/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testonlineconnectdb;

import java.io.*;
import java.util.ArrayList;
import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author AngelKid
 */
public class BankQuestion {

    public SaxProcessUpdateQuestion sax;
    public BankQuestion()
    {
        sax = new SaxProcessUpdateQuestion();
    }
    public void Update(String pathBankQuestion,Question q) {
        try {
            File file = new File(pathBankQuestion);
            //Create instance of DocumentBuilderFactory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            //Get the DocumentBuilder
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            //Using existing XML Document
            Document doc = docBuilder.parse(file);
            //create the root element
            Element root = doc.getDocumentElement();
            //create child element
            Element nodeQuestion = doc.createElement("CauHoi");
            Element childElement0 = doc.createElement("NoiDung");
            Element childElement1 = doc.createElement("LuaChon");
            Element childElement2 = doc.createElement("LuaChon");
            Element childElement3 = doc.createElement("LuaChon");
            Element childElement4 = doc.createElement("LuaChon");
            Element childElement5 = doc.createElement("MaCauHoi");
            Element childElement6 = doc.createElement("DapAn");
            Element childElement7 = doc.createElement("DoKho");
            childElement0.setTextContent(q.ask);
            childElement1.setTextContent(q.choice1);
            childElement2.setTextContent(q.choice2);
            childElement3.setTextContent(q.choice3);
            childElement4.setTextContent(q.choice4);
            childElement5.setTextContent(q.code);
            childElement6.setTextContent(q.result);
            childElement7.setTextContent(q.testType);
            nodeQuestion.appendChild(childElement0);
            nodeQuestion.appendChild(childElement1);
            nodeQuestion.appendChild(childElement2);
            nodeQuestion.appendChild(childElement3);
            nodeQuestion.appendChild(childElement4);
            nodeQuestion.appendChild(childElement5);
            nodeQuestion.appendChild(childElement6);
            nodeQuestion.appendChild(childElement7);
            root.appendChild(nodeQuestion);
            //Add the attribute to the child
            //childElement.setAttribute("id","3");


            //root.appendChild(childElement);

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();

            OutputStream f0;
            byte buf[] = xmlString.getBytes();
            f0 = new FileOutputStream(pathBankQuestion);
            for (int i = 0; i < buf.length; i++) {
                f0.write(buf[i]);
            }
            f0.close();
            buf = null;
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
    public void UpdateProcessing(String pathXMLUpdate, String pathBankQuestion) throws IOException
    {
        //GetMaxCode
        CountNumberAndMaxQuestion(pathBankQuestion);
        int maxCode = sax.GetMaxCode();
        //GetUpdateData
        SaxProcessReadUpdate sax2 = new SaxProcessReadUpdate();
        SAXParserFactory saxpaser2=SAXParserFactory.newInstance(); 
        try { 
            SAXParser parser = saxpaser2.newSAXParser(); 
            File F2=new File(pathXMLUpdate); 
            parser.parse(F2, sax2); 
        } catch (ParserConfigurationException ex) { 
            
        } catch (SAXException ex) { 
            
        }  
        ArrayList questions = sax2.GetUpdateXMLData();
        if(questions == null)
        {
            System.out.println("Data invalid");
            return ;
        }
        System.out.println("Size: " + questions.size());
        int indexCode = maxCode;
        for(int i = 0; i < questions.size();i++)
        {
            Question q = (Question)questions.get(i);
            indexCode++;
            q.code      =   Integer.toString(indexCode);
            Update(pathBankQuestion,q);
        }
        
    }
    public void CountNumberAndMaxQuestion (String pathBankQuestion) throws IOException
    {
        SAXParserFactory saxpaser=SAXParserFactory.newInstance(); 
        try { 
            SAXParser parser = saxpaser.newSAXParser(); 
            File F=new File(pathBankQuestion); 
            parser.parse(F, sax); 
        } catch (ParserConfigurationException ex) { 
            
        } catch (SAXException ex) { 
            
        }  
    }
    public int GetMaxCode()
    {
        return sax.maxCode;
    }    
    public ArrayList GetEasyQuestion()
    {
        return sax.GetEasyQuestion();
    }
    public ArrayList GetNormalQuestion()
    {
        return sax.GetNormalQuestion();
    }
    public ArrayList GetDifficultQuestion()
    {
        return sax.GetDifficultQuestion();
    }
}
